<?php


class UserTworldVoucherModel extends BaseModel
{
    protected $table = 'yf_user_tworld_voucher';

    protected $principal = 'id';

    /**
     * 状态
     */
    const STATUS = [
        'normal' => 1,//正常
        'overdue' => 2,//过期
        'used' => 3, //已使用
        'back' => 4,//退卡
        'unusual' => -1 //异常
    ];

    /**
     * @param int $user_id
     * @return array|bool|mixed
     */
    public function getListByUser(int $user_id, int $user_tworld_card_id)
    {
        $sql = "select v.id, v.status, v.expiration_time,v.createtime, i.title, i.price, i.category, i.vaild_type 
from {$this->table} as v 
left join yf_tworld_voucher as tv on v.tworld_voucher_id = tv.id
left join yf_tworld_voucher_item as i on tv.tworld_voucher_item_id = i.id
where v.user_id = :user_id and v.is_del = 0 and v.user_tworld_card_id = :user_tworld_card_id";
        return $this->db->query($sql, [
            ':user_id' => $user_id,
            ':user_tworld_card_id' => $user_tworld_card_id
        ])->fetchAll(PDO::FETCH_ASSOC) ?? [];
    }

    /**
     * 获取单个会员卡优惠券详情
     * @param int $id
     * @return array|mixed
     */
    public function getVoucherInfo(int $id)
    {
        $sql = "select v.id, v.user_id, v.status, v.expiration_time,v.createtime, i.title, i.price, i.category, i.vaild_type, i.item_code 
from {$this->table} as v 
left join yf_tworld_voucher as tv on v.tworld_voucher_id = tv.id
left join yf_tworld_voucher_item as i on tv.tworld_voucher_item_id = i.id
where v.id = :id";
        return $this->db->query($sql, [
                ':id' => $id,
            ])->fetch(PDO::FETCH_ASSOC) ?? [];
    }

}